#include "scdatathread.h"

ScDataThread::ScDataThread()
{

}

void ScDataThread::setConfig(int x, int y, int width, int height, int fps, int bit, NetCore *netCore)
{
    this->netCore = netCore;
    this->indexX = x;
    this->indexY = y;
    this->scWidth = width;
    this->scHeight = height;
    this->fps = fps;
    rdp.setConfig(indexX, indexY, scWidth, scHeight, bit);
}

void ScDataThread::run()
{
    m_running = true;

    qDebug() <<(QString("接收配置信息：[宽度:%1,高度:%2,X轴分块:%3,Y轴分块:%4,刷新率:%5]")
                               .arg(scWidth).arg(scHeight).arg(indexX).arg(indexY).arg(fps));

    while (1) {
        // 收到退出信号
        if(!m_running)
        {
            break;
        }

        // 做一次全屏截图
        rdp.takeScreenShot();

        // 循环发送变化的截屏位图块
        for(int y = 0; y < indexY ; y ++)
        {
            for(int x = 0; x < indexX ; x ++)
            {
                QByteArray pixmapData = rdp.getBlockMapData(x, y);
                if(!pixmapData.isEmpty())
                {
                    // 通知上层UI工作进度
                    emit this->sigUImsg(QString("正在传输图像数据[%1,%2] 数据大小：%3").arg(x).arg(y).arg(pixmapData.length()));
                    netCore->sendScData(x, y, pixmapData);
                }
            }
        }
        msleep(fps);
    }
}

void ScDataThread::stop()
{
    m_running = false;
}
